package com.example.harmonyosproject.server.service.impl;

import com.example.harmonyosproject.Utils.PinyinUtils;
import com.example.harmonyosproject.pojo.AddFriend;
import com.example.harmonyosproject.pojo.Friend;
import com.example.harmonyosproject.pojo.Message;
import com.example.harmonyosproject.pojo.Result;
import com.example.harmonyosproject.server.mapper.FriendMapper;
import com.example.harmonyosproject.server.mapper.MessageMapper;
import com.example.harmonyosproject.server.mapper.UserMapper;
import com.example.harmonyosproject.server.service.FriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Slf4j
@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    FriendMapper friendMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MessageMapper messageMapper;

    @Override
    public void addFriend(AddFriend addFriend) {
        String name=userMapper.getName(addFriend.getUserId());
        AddFriend addFriend2=new AddFriend(
                addFriend.getFriendId(),
                addFriend.getUserId(),
                (short) 2,
                addFriend.getText(),
                name
        );
        friendMapper.InsertAddFriend(addFriend);
        friendMapper.InsertAddFriend(addFriend2);

    }

    @Override
    public void agreeFriend(AddFriend addFriend) {
        addFriend.setState((short) 3);
        AddFriend addFriend2=new AddFriend(
                addFriend.getFriendId(),
                addFriend.getUserId(),
                (short) 3
        );
        friendMapper.updateAddFriend(addFriend);
        friendMapper.updateAddFriend(addFriend2);
    }

    @Override
    public void changeNickname(Friend friend) {
        friendMapper.updateFriend(friend);
        AddFriend addFriend2=new AddFriend(
                friend.getUserId(),
                friend.getFriendId(),
                friend.getNickname()
        );
        friendMapper.updateAddFriend(addFriend2);
    }

    @Override
    public void DeleteFriend(Friend friend) {
        Friend friend2=new Friend(
                friend.getFriendId(),
                friend.getUserId(),
                (short) 2
        );
        friendMapper.updateFriend(friend2);
        friendMapper.deleteFriend(friend);
    }

    @Override
    public List<Friend> GetAllFriend(Integer id) {
        List<Friend> friendList =friendMapper.GetAllFriend(id);
        int i=0;
        for(Friend friend:friendList){
            Message message =messageMapper.getLast(friend);
            String image =userMapper.getImage(friend.getFriendId());
            String text;
            if(message==null){
                text=null;
            }
            else {
                if (message.getText() != null&&message.getText() !="") {
                    text = message.getText();
                } else {
                    text = "[图片]";
                }
            }

            friend.setLastMessage(text);
            friend.setImage(image);
            friendList.set(i,friend);
            i++;
        }
        return friendList;
    }

    @Override
    public List<AddFriend> getAllAddFriend(Integer id) {
        List<AddFriend> addFriends = friendMapper.getAllAddFriend(id);
        int i=0;
        for(AddFriend addFriend:addFriends){
            String headImage=userMapper.getImage(addFriend.getFriendId());
            addFriend.setHeadImage(headImage);
            addFriends.set(i,addFriend);
            i++;
        }
        return addFriends;
    }

    @Override
    public AddFriend getAddFriend(Integer id) {
        AddFriend addFriend=friendMapper.getAddFriend(id);
        return addFriend;
    }

    @Override
    public Object[][] getContact(Integer id) {

        List<Friend> friendList=GetAllFriend(id);
        List<List<Friend>> friendList1=new ArrayList<>();
        for (int i = 0; i < 27; i++) {
            List<Friend> a=new ArrayList<>();
            friendList1.add(a);
        }
        for(Friend friend:friendList){
            int index=PinyinUtils.getFirstLetter(friend.getNickname())-'a';
            if(index<0||index>25)
                index=26;
            friendList1.get(index).add(friend);
        }

        Object[][] friendArray = new Object[27][];

        for (int i = 0; i < friendList1.size(); i++) {
            List<Friend> innerList = friendList1.get(i);
            friendArray[i] = new Friend[innerList.size()];
            for (int j = 0; j < innerList.size(); j++) {
                friendArray[i][j] = innerList.get(j);
            }
        }

        return friendArray;
    }

    @Override
    public Friend getFriend(Friend friend) {
        Friend friend1=friendMapper.getFriend(friend);
        return friend1;
    }
}
